home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 721 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. From: sdouglas@armltd.co.uk (scott douglass)
  2. Message-ID: <sdouglas-1403961511110001@193.131.176.202>
  3. X-Original-Date: Thu, 14 Mar 1996 15:11:11 +0000
  4. Path: in2.uu.net!bounce-back
  5. Date: 14 Mar 96 17:02:20 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Two questions about declarations in conditions
  9. Organization: Apple Computer, Inc.
  10. X-Newsreader: Yet Another NewsWatcher 2.0.2
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMUhU8+EDnX0m9pzZAQFTbwF+LQxkylI5X4NRYqDYx1/VL94rak8aKkIb
  13.     NwW5XKobI3Jg2TeiqRohKlMSowwJ1pem
  14.     =UXRO
  15.  
  16. Hello,
  17.  
  18. I read section 6.4 carefully but I couldn't decide what the lifetime (not
  19. scope) of an object declared in the condition of a while or for statements
  20. is.  Reading D&E 3.11.5.2 didn't help either.  Given the following:
  21.  
  22. struct T { T(int); ~T(); operator bool() const; /*...*/ };
  23.  
  24. void f(int i)
  25.     {
  26.     while (T t = i) { /* do something with 't' */ }
  27.     }
  28.  
  29. There are two "obvious" possibilities, I9m leaning toward the first:
  30.  
  31. 1 -- The object is initialized just once and destroyed just once, making
  32. 'f' above eqivalent to:
  33.  
  34. void f(int i)
  35.     {
  36.         {
  37.         T t = i;
  38.         while (t) { /* do something with 't' */ }
  39.         }
  40.     }
  41.  
  42. 2 -- the object is initialized and destroyed on each iteration, making 'f'
  43. above eqivalent to:
  44.  
  45. void f(int i)
  46.     {
  47.     while (1) { T t = i; if (!t) break; /* do something with 't' */ }
  48.     }
  49.  
  50. Which is it supposed to be?
  51.  
  52. Bonus question:  why does the grammer allow only the '=
  53. assignment-expression' form:
  54.  
  55.           condition:
  56.                   expression
  57.                   type-specifier-seq declarator = assignment-expression
  58.  
  59. instead of:
  60.  
  61.           condition:
  62.                   expression
  63.                   type-specifier-seq declarator = assignment-expression
  64.                   type-specifier-seq declarator ( expression-list )
  65.  
  66. So that I could write:
  67.  
  68. void f(int i)
  69.     {
  70.     while (T t(i)) { /* do something with 't' */ }
  71.     }
  72.  
  73. Which I prefer.
  74.  
  75. [I was tempted to suggest the tidier
  76.  
  77.           condition:
  78.                   expression
  79.                   type-specifier-seq declarator initializer
  80.  
  81. but that would allow the potentially disagreeable '= { /*...*/ }' form.]
  82.  
  83. Thanks for your kind attention,
  84.     --scott
  85. ---
  86. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  87. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  88. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  89. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  90. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  91.